﻿#nullable enable
using System;
using System.Buffers;
using MemoryPack;

namespace Hsenl.Network {
    public static class NetworkSerializer {
        public static void SerializeOfMemoryPack<T>(in IBufferWriter<byte> bufferWriter, in T? value, MemoryPackSerializerOptions? options = default) {
            try {
                SerializeHelper.SerializeOfMemoryPack(bufferWriter, value, options);
            }
            catch (Exception e) {
                throw new ParseMessageException(e.Message);
            }
        }

        public static byte[] SerializeOfMemoryPack<T>(in T? value, MemoryPackSerializerOptions? options = default) {
            try {
                return SerializeHelper.SerializeOfMemoryPack(value, options);
            }
            catch (Exception e) {
                throw new ParseMessageException(e.Message);
            }
        }

        public static T? DeserializeOfMemoryPack<T>(ReadOnlySpan<byte> buffer, MemoryPackSerializerOptions? options = default) {
            try {
                return SerializeHelper.DeserializeOfMemoryPack<T>(buffer, options);
            }
            catch (Exception e) {
                throw new ParseMessageException(e.Message);
            }
        }

        public static int DeserializeOfMemoryPack<T>(ReadOnlySpan<byte> buffer, ref T? value, MemoryPackSerializerOptions? options = default) {
            try {
                return SerializeHelper.DeserializeOfMemoryPack(buffer, ref value, options);
            }
            catch (Exception e) {
                throw new ParseMessageException(e.Message);
            }
        }
    }
}